package handle.data;

import save.data.blogContentBean;

import utils.DataBaseUtil;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


@WebServlet(name = "detail", value = "/detail")
public class detail extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Connection con = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        //博客具体内容
        String title;
        String blogContent;
        String createTime;
        String publisher;
        int blogId = Integer.parseInt(request.getParameter("bolgId"));
        blogContentBean content=null;
        try {
            content = (blogContentBean) session.getAttribute("content");
            if (content==null){
                content = new blogContentBean();
                session.setAttribute("content",content);
            }
        }catch (Exception e){}

        try {

            con  = DataBaseUtil.getConnection();
            String SQL =
                    "select title,publisher,createTime,content from blog_content where blogId=?";
            preparedStatement = con.prepareStatement(SQL);
            preparedStatement.setInt(1, blogId);
            rs = preparedStatement.executeQuery();//查blog_content和user表。//获得博客具体内容（多表查询）
            rs.next();
            title = rs.getString("title");
            blogContent = rs.getString("content");
            createTime = rs.getString("createTime");
            publisher = rs.getString("publisher");
            content.setBlogContent(blogContent);
            content.setBlogId(blogId);
            content.setPublisher(publisher);
            content.setTitle(title);
            content.setCreateTime(createTime);


            request.getRequestDispatcher("detail.jsp").forward(request,response);
        }

        catch (Exception e){
            System.out.println("e = " + e);
        }
        finally {

            DataBaseUtil.close(rs,null ,con,preparedStatement);

        }

    }
}
